#include "graphicstextitem.h"
#include <QtWidgets>
#include <QDebug>
#include "itemmimedata.h"
#include "times.h"
#include <QThread>
#include "dragdrop.h"

GraphicsTextItem::GraphicsTextItem(QGraphicsItem *parent)
	: QGraphicsTextItem(parent)
{
	setPlainText(tr("hello qt"));
	setFlags(QGraphicsItem::ItemIsSelectable
		 //| QGraphicsItem::ItemIsMovable
		);
	
	//setTextInteractionFlags(Qt::TextEditorInteraction);

	//QGraphicsTextItem * item = new QGraphicsTextItem(tr("child text item"));
	//item->setParentItem(this);
}

GraphicsTextItem::~GraphicsTextItem()
{

}

// --- override; ------------------------
void GraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
	setTextInteractionFlags(Qt::TextEditorInteraction);
	//setSelected(true);
	
}
// drag & drop;
void GraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	Times::outCurrentTime("textItem");
	qDebug() << "->GraphicsTextItem.mousePressEvent() enter."
		<<"("<<event->pos().x()<<","<<event->pos().y()<<")";
	QThread::usleep(1);

	//if (event->button() == Qt::LeftButton)
	//	startPos = event->pos();
	if(gDragDrop->itemMousePressEvent(event, this))
		return;

	QGraphicsTextItem::mousePressEvent(event);
}
void GraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
	//if (event->buttons() & Qt::LeftButton) {
	//	int distance = (event->pos() - startPos).manhattanLength();
	//	if (distance >= QApplication::startDragDistance()){
	//		qDebug("->GraphicsTextItem.mouseMoveEvent(): distance ok.");
	//		performDrag();
	//	}
	//}
	if( gDragDrop->itemMouseMoveEvent(event,this))
		return;

	QGraphicsTextItem::mouseMoveEvent(event);
}

void GraphicsTextItem::focusOutEvent(QFocusEvent *event)
{
	setTextInteractionFlags(Qt::NoTextInteraction);
}